package mlp.game.shadownbolts;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;

public class Projectile extends Renderable {
	public boolean firing;
	public Spitfire player;
	public Shadowbolt enemy;
	public ProjectileCollector target;
	
	private Paint colorHue = new Paint();
	private int hueShift = 0;
	
	public Projectile(Sprite sprite){
		super(sprite);
		colorHue.setColorFilter(ColorFilterGenerator.adjustHue(hueShift));
	}
	
	public void fire(){
		setPosition(
				new PointF(
						getPosition().x + (enemy.getFrame().right - getPosition().x),
						getPosition().y + (enemy.getPosition().y - getPosition().y)
						)
				);
		hueShift = (int) (Math.random() * 360) - 180;
		colorHue.setColorFilter(ColorFilterGenerator.adjustHue(hueShift));
		this.firing = true;
		
	}
	
	public void update(long gameTime){
		if(this.firing == false){
			setPosition(
					new PointF(
							getPosition().x + (enemy.getFrame().right - getPosition().x),
							getPosition().y + (enemy.getPosition().y - getPosition().y)
							)
					);
		} else if(this.firing == true){
			setPosition(
					new PointF(
							getPosition().x + (target.getX() - getPosition().x) / 15,
							getPosition().y + (player.getFrame().centerY() - getPosition().y) / 25
							)
					);

		}
		if(player.isHit(this) && player.defence == true){
			this.firing = false;
		} else if(player.isHit(this) && player.defence == false){
			player.takeHit();
			this.firing = false;
		}
		if(RectF.intersects(target.rect, getFrame()) || getPosition().x < -50){
			this.firing = false;
		}
		
		hueShift += 3;
		if(hueShift > 180){
			hueShift = -180;
		}
		colorHue.setColorFilter(ColorFilterGenerator.adjustHue(hueShift));
		
		super.update(gameTime);
	}
	
	@Override
	public void draw(Canvas canvas) {
		if(this.firing){
			super.draw(canvas, colorHue);
		}
	}
}
